
package com.iceman.secretsms;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.test.suitebuilder.annotation.SmallTest;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class SmsActivity extends Activity {
    private String number;

    private Sms[] mSmsList;

    private RmsAdapter rms;
    
    private Button mSendButton;
    
    private EditText mEditSms;

    private ListView mSmsListView;

    private SmsListAdapter mSmsListAdapter;
    
    private SimpleDateFormat mDateFormat;
    
    private SmsManager mSmsManager;
    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sms_layout);
        Intent it = getIntent();
        number = it.getStringExtra("number");
        rms = RmsAdapter.getInstance(this);
        mSmsList = rms.getSmsArray(number);
        mDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        mSmsListView = (ListView)findViewById(R.id.sms_list);
        mSmsListAdapter = new SmsListAdapter();
        mSmsListView.setAdapter(mSmsListAdapter);
        mSmsManager = SmsManager.getDefault();
        mSendButton = (Button)findViewById(R.id.send_sms);
        mEditSms = (EditText)findViewById(R.id.edit_text);
        mSendButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mSmsManager.sendTextMessage(number, null, mEditSms.getEditableText().toString(), null, null);
                Sms sms = new Sms(number, System.currentTimeMillis(), "out", mEditSms.getEditableText().toString());
                rms.putSms(sms);
            }
        });
    }

    class SmsListAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            if (mSmsList != null) {
                return mSmsList.length;
            } else {
                return 0;
            }
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView==null){
                convertView = getLayoutInflater().inflate(R.layout.sms_list_item_layout, null);
            }
            TextView content = (TextView) convertView.findViewById(R.id.sms_content);
            content.setText(mSmsList[position].getText());
            TextView info = (TextView) convertView.findViewById(R.id.sms_info);
            info.setText(mDateFormat.format(new Date(mSmsList[position].getTime())));
            if(mSmsList[position].getType().equals("in")){
                convertView.setBackgroundResource(R.drawable.sms_in_bg);
            }else{
                convertView.setBackgroundResource(R.drawable.sms_out);
            }
            return convertView;
        }

    }
}
